package com.codefish.controller;


import com.codefish.entity.CapitalAccount;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.CapitalAccountService;
import com.codefish.util.Assert;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 账户信息
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-07-03 22:05:24
 */
@RestController
@RequestMapping("/capitalAccount")
public class CapitalAccountController {
    @Autowired
    private CapitalAccountService capitalAccountService;


    /**
     * 新增单个
     *
     * @param token
     * @param capitalAccount
     * @return
     */
    @PostMapping("/saveOne")
    public R saveOne(@RequestHeader("Authentication") String token, @RequestBody CapitalAccount capitalAccount) {
        Assert.isNull(capitalAccount.getAccountName(), "accountName不能为空");

        if (StringUtils.isEmpty(capitalAccount.getMoneyAmount())) {
            return R.error("moneyAmount不能为空");
        }
        SysUserTokenEntity user = JWTService.getUser(token);
        if (ObjectUtils.isEmpty(user.getId())) {
            return R.error("当前用户无效");
        }

        // set createId / createTime
        capitalAccount.setCreateId(user.getId());
        capitalAccount.setCreateTime(new Date());
        capitalAccount.setFamilyId(user.getFamilyId());

        // 确认是否可以新增
        boolean checkFlag = capitalAccountService.checkFlag(capitalAccount);
        if (!checkFlag){
            return R.error("已经存在该资金账户，不可继续添加");
        }

        int id = capitalAccountService.saveOne(capitalAccount);
        if (-1 == id){
            return R.error("新增失败");
        }
        return R.ok("新增成功").put("data",id);
    }

    /**
     * 修改单个
     *
     * @param token
     * @param capitalAccount
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestHeader("Authentication") String token, @RequestBody CapitalAccount capitalAccount) {

        Assert.isNull(capitalAccount.getId(), "id不能为空");
        Assert.isNull(capitalAccount.getAccountName(), "accountName不能为空");
        Assert.isNull(capitalAccount.getMoneyAmount(), "moneyAmount不能为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        if (ObjectUtils.isEmpty(user.getId())) {
            return R.error("当前用户无效");
        }
        // set updateId / updateTime
        capitalAccount.setUpdateId(user.getId());
        capitalAccount.setUpdateTime(new Date());
        capitalAccount.setFamilyId(user.getFamilyId());


        // 确认是否可以新增
        boolean checkFlag = capitalAccountService.checkFlag(capitalAccount);
        if (!checkFlag){
            return R.error("已经存在该资金账户，不可继续修改");
        }

        boolean updateFlag = capitalAccountService.updateById(capitalAccount);

        if (!updateFlag) {
            return R.error("修改失败");
        }

        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam("id") Integer id) {

        boolean deleteFlag = capitalAccountService.deleteOne(id);
        if (!deleteFlag) {
            return R.error("删除失败");
        }

        return R.ok();
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/getAll")
    public R getAll(@RequestHeader("Authentication")String token) {

        SysUserTokenEntity user = JWTService.getUser(token);
        if (ObjectUtils.isEmpty(user.getFamilyId())) {
            return R.error("当前用户无效");
        }

        List<CapitalAccount> retList = capitalAccountService.getAll(user.getFamilyId());

        return R.ok().put("data", retList);
    }

}
